package com.pgytesting.mobilecertpay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pgytesting.mobilecertpay.entity.NaCaptchaInformation;
import com.pgytesting.mobilecertpay.dao.NaCaptchaInformationDao;
import com.pgytesting.mobilecertpay.service.NaCaptchaInformationService;
import com.pgytesting.mobilecertpay.util.UtilMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 验证码信息表(NaCaptchaInformation)表服务实现类
 *
 * @author makejava
 * @since 2019-05-10 10:22:59
 */
@Slf4j
@Service("naCaptchaInformationService")
public class NaCaptchaInformationServiceImpl implements NaCaptchaInformationService {
    @Resource
    private NaCaptchaInformationDao naCaptchaInformationDao;


    /**
     * 通过ID查询单条数据
     *
     * @param naCaptchaInformation 参数
     * @return 实例对象
     */
    @Override
    public NaCaptchaInformation queryById(NaCaptchaInformation naCaptchaInformation) {
        return this.naCaptchaInformationDao.queryById(naCaptchaInformation);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<NaCaptchaInformation> queryAllByLimit(int offset, int limit) {
        return this.naCaptchaInformationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 条件查询多条数据
     *
     * @param naCaptchaInformation 实例对象
     * @return 对象列表
     */
    @Override
    public List<NaCaptchaInformation> queryAll(NaCaptchaInformation naCaptchaInformation) {
        return this.naCaptchaInformationDao.queryAll(naCaptchaInformation);
    }

    /**
     * 新增数据
     *
     * @param jsonObject 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String, Object> insert(JSONObject jsonObject) {
        NaCaptchaInformation naCaptchaInformation = new NaCaptchaInformation();
        Map<String, Object> resultMap = new LinkedHashMap<>();
        log.info("进行验证码查询，是否存已在");
        naCaptchaInformation.setCaptchaType(String.valueOf(jsonObject.get("outOrderId")));
        naCaptchaInformation.setMerchantMail(String.valueOf(jsonObject.get("merchantMail")));
        NaCaptchaInformation naCapt = this.naCaptchaInformationDao.queryById(naCaptchaInformation);
        if (naCapt == null) {
            log.info("初始获取");
            naCaptchaInformation.setMailCode(UtilMethod.randomCode());
            naCaptchaInformation.setCurrentTime(UtilMethod.unixLong());
            naCaptchaInformation.setNumberTimes(1);
            try {
                this.naCaptchaInformationDao.insert(naCaptchaInformation);
                resultMap.put("respCode","200");
                resultMap.put("merchantMail",jsonObject.get("merchantMail"));
                resultMap.put("respMsg","您的验证码为：" + naCaptchaInformation.getMailCode() + " ，短信验证码有效期10分钟，请尽快完成验证");
            }catch (Exception e){
                log.error("新增短信验证码异常" + e.getMessage());
                resultMap.put("respCode","500");
            }

        } else {
            log.info("重复获取");
            naCapt.setMailCode(UtilMethod.randomCode());
            naCapt.setCurrentTime(UtilMethod.unixLong());
            naCapt.setNumberTimes(naCapt.getNumberTimes() + 1);
            try {
                this.naCaptchaInformationDao.update(naCapt);
                resultMap.put("respCode","200");
                resultMap.put("merchantMail",jsonObject.get("merchantMail"));
                resultMap.put("respMsg","您的验证码为：" + naCapt.getMailCode() + " ，短信验证码有效期10分钟，请尽快完成验证");
            }catch (Exception e){
                log.error("重复获取短信验证码异常" + e.getMessage());
                resultMap.put("respCode","500");
            }
        }
        return resultMap;
    }

    /**
     * 修改数据
     *
     * @param naCaptchaInformation 实例对象
     * @return 实例对象
     */
    @Override
    public NaCaptchaInformation update(NaCaptchaInformation naCaptchaInformation) {
        this.naCaptchaInformationDao.update(naCaptchaInformation);
        return this.queryById(naCaptchaInformation);
    }

    /**
     * 通过主键删除数据
     *
     * @param merchantMail 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String merchantMail) {
        return this.naCaptchaInformationDao.deleteById(merchantMail) > 0;
    }
}